<template>
  <div class="upload-img">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-imgerror :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除按钮 -->
    <i v-if="value" class="el-icon-circle-close close" @click="del" />
    <!-- 预览按钮 -->
    <i v-if="value" class="el-icon-zoom-in big" @click="isShow = true" />
    <!-- 大图片弹框 -->
    <el-dialog title="预览" :visible.sync="isShow">
      <img v-imgerror :src="value" alt="" class="img">
    </el-dialog>
    <!-- 进度条 -->
    <el-progress
      v-if="showProgress"
      :width="178"
      class="progress"
      type="circle"
      :percentage="percentage"
    />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDzsYQ4UeYDguscIxQffQwEI6fxzoOjodC',
  SecretKey: 'N5YliPq0uYzBAuPfgS1NUM0R5XnoeHgs'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imageUrl: '',
      isShow: false,
      percentage: 0, // 0-100范围
      showProgress: false
    }
  },
  methods: {
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义上传
    httpRequest(res) {
      this.showProgress = true
      cos.putObject(
        {
          Bucket: 'abc-1258719776' /* 必须:存储桶 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: res.file.name /* 必须 存储的文件名*/,
          StorageClass: 'STANDARD', // 不用修改的字段
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              this.showProgress = false
            }
            // 进度数据
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 接口返回值内有上传文件地址
          // 没有错误就是成功
          // err:错误
          // data:成功后的数据
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
          console.log(err || data)
        }
      )
    },
    // 删除
    del() {
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
// scoped:只管当前组件和子组件最外层
// innerHTML内的class能不能   不能
// 看得到的就管得到，看不到的管不到
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.upload-img {
  position: relative;
  width: 178px;
  &:hover {
    .close,
    .big {
      display: inline-block;
    }
  }
  .close {
    position: absolute;
    left: 136px;
    top: 1px;
    font-size: 33px;
    color: red;
    display: none;
    cursor: pointer;
  }
  .big {
    position: absolute;
    left: 96px;
    top: 1px;
    font-size: 33px;
    color: red;
    display: none;
    cursor: pointer;
  }
  .img {
    width: 100%;
  }
  .progress {
    position: absolute;
    top: 0;
    left: 0;
    background-color: #fff;
  }
}
</style>
